package com.gtw.distribution;

import com.gtw.common.utils.WeiXinMsgUtil;
import com.gtw.modules.api.service.*;
import com.gtw.modules.entity.*;
import com.gtw.modules.sys.service.SysCommissionRatioService;
import com.gtw.modules.sys.service.SysTbOrderService;
import com.gtw.modules.sys.service.SysTbRaidDetailsService;
import com.gtw.modules.sys.service.TbProxyDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 二级复二级分销结算
 * Created by wuye on 2018/3/7.
 */
@Service
public class DistributionUtil {

    @Autowired
    private SysTbOrderService sysTbOrderService;

    @Autowired
    private SysTbRaidDetailsService sysTbRaidDetailsService;

    @Autowired
    private TbUserService tbUserService;

    @Autowired
    private TbProxyDetailsService tbProxyDetailsService;

    @Autowired
    private TbCharmvalueNotesService tbCharmvalueNotesService;

    @Autowired
    private SysCommissionRatioService sysCommissionRatioService;

    @Autowired
    private IntegralClearingUtil integralClearingUtil;



    /**
     * 判断类型
     *
     * @return
     */
    public boolean iftype(TbOrder tbOrder) {
        boolean isok = false;
//        //查询用户，判断是否是会员
//        TbUser tbUser = tbUserService.queryByUid(tbOrder.getUid());
//        if (tbOrder.getChannel().equals("1")) {
//            isok = this.distribution3(tbOrder);
//        } else {
//            if (tbUser.getType() == 1) {
//                isok = this.distribution(tbOrder);
//            }
//        }
        if (tbOrder.getChannel().equals("1")) {
            if (tbOrder.getRemark().equals("团长")) {
                isok = this.distribution3(tbOrder);
            }
        } else {
            isok = this.distribution(tbOrder);
        }
        this.distribution4(tbOrder);
        return isok;
    }

    /**
     * 会员二级复二级分销结算
     *
     * @param order
     * @return
     */
    public boolean distribution(TbOrder order) {
        ReentrantLock lock = new ReentrantLock();
        List<TbOrder> orderList = sysTbOrderService.queryByUid(order.getUid());
        Calendar calendar = Calendar.getInstance();
        try {
            lock.lock();
            //判断订单是否为首购
            TbUser user00 = tbUserService.queryByUid(order.getUid());
            if (user00.getType() == 0) {
                /*
                首购
                 */
                //订单金额
                Float money = order.getTotalfee() - order.getFreight();
                TbCommissionRatio tbCommissionRatio = sysCommissionRatioService.queryByUserFisrt();
                //发起者 E
                TbUser user0 = tbUserService.queryByUid(order.getUid());
                if (user0 != null) {
                    //第一级受益者
                    if(user0.getParentid() != null){
                    TbUser user1 = tbUserService.queryById(user0.getParentid());
                    if (user1 != null) {
                        //计算第一级受益者应得金额
                        Integer money1 = (int) (money * tbCommissionRatio.getRatio());
                        user1.setMemberbalance(user1.getMemberbalance() + money1);
                        user1.setBalance(user1.getBalance() + money1);
                        String money1str=money1/100d+"元";
                        WeiXinMsgUtil.yongjinMsg(user1.getWxopenid(),money1str);
                        tbUserService.update(user1);
                        //抽佣明细
                        TbRaidDetails tbRaidDetails1 = new TbRaidDetails();
                        tbRaidDetails1.setAmount(money1);
                        tbRaidDetails1.setBenefitUserid(user1.getUserId());
                        tbRaidDetails1.setBenefitUsername(user1.getUsername());
                        tbRaidDetails1.setTriggerUserid(user0.getUserId());
                        tbRaidDetails1.setTriggerUsername(user0.getUsername());
                        tbRaidDetails1.setTriggerImg(user0.getProfileimageurl());
                        tbRaidDetails1.setCreatetime(new Date());
                        tbRaidDetails1.setMonth(calendar.get(Calendar.MONTH) + 1);
                        tbRaidDetails1.setOrderid(order.getId());
                        sysTbRaidDetailsService.save(tbRaidDetails1);
                        if (user1.getParentid() != null) {
                            //第二级受益者
                            TbUser user2 = tbUserService.queryById(user1.getParentid());
                            if (user2 != null) {
                                Integer money2 = (int) (money * tbCommissionRatio.getSecondratio());
                                user2.setMemberbalance(user2.getMemberbalance() + money2);
                                user2.setBalance(user2.getBalance() + money2);
                                //通知抽成佣金
                                String money2str=money2/100d+"元";
                                WeiXinMsgUtil.yongjinMsg(user2.getWxopenid(),money2str);

                                tbUserService.update(user2);
                                //抽佣明细
                                TbRaidDetails tbRaidDetails2 = new TbRaidDetails();
                                tbRaidDetails2.setAmount(money2);
                                tbRaidDetails2.setBenefitUserid(user2.getUserId());
                                tbRaidDetails2.setBenefitUsername(user2.getUsername());
                                tbRaidDetails2.setTriggerUserid(user0.getUserId());
                                tbRaidDetails2.setTriggerUsername(user0.getUsername());
                                tbRaidDetails2.setTriggerImg(user0.getProfileimageurl());
                                tbRaidDetails2.setCreatetime(new Date());
                                tbRaidDetails2.setMonth(calendar.get(Calendar.MONTH) + 1);
                                tbRaidDetails2.setOrderid(order.getId());
                                sysTbRaidDetailsService.save(tbRaidDetails2);
                /*-------------------------------------------------------------------------------*/
                                if (user2.getParentid() != null) {
                                    //第三级受益者
                                    TbUser user3 = tbUserService.queryById(user2.getParentid());
                                    if (user3 != null) {
                                        Integer money3 = (int) ((money1 + money2) * tbCommissionRatio.getThirdratio());
                                        user3.setMemberbalance(user3.getMemberbalance() + money3);
                                        user3.setBalance(user3.getBalance() + money3);
                                        tbUserService.update(user3);

                                        //通知抽成佣金
                                        String money3str=money3/100d+"元";
                                        WeiXinMsgUtil.yongjinMsg(user3.getWxopenid(),money3str);
                                        //抽佣明细
                                        TbRaidDetails tbRaidDetails3 = new TbRaidDetails();
                                        tbRaidDetails3.setAmount(money3);
                                        tbRaidDetails3.setBenefitUserid(user3.getUserId());
                                        tbRaidDetails3.setBenefitUsername(user3.getUsername());
                                        tbRaidDetails3.setTriggerUserid(user0.getUserId());
                                        tbRaidDetails3.setTriggerUsername(user0.getUsername());
                                        tbRaidDetails3.setTriggerImg(user0.getProfileimageurl());
                                        tbRaidDetails3.setCreatetime(new Date());
                                        tbRaidDetails3.setMonth(calendar.get(Calendar.MONTH) + 1);
                                        tbRaidDetails3.setOrderid(order.getId());
                                        sysTbRaidDetailsService.save(tbRaidDetails3);

                                        if (user3.getParentid() != null) {
                                            //第四级受益者
                                            TbUser user4 = tbUserService.queryById(user3.getParentid());
                                            if (user4 != null) {
                                                Integer money4 = (int) (money2 * tbCommissionRatio.getFourthratio());
                                                user4.setMemberbalance(user4.getMemberbalance() + money4);
                                                user4.setBalance(user4.getBalance() + money4);
                                                tbUserService.update(user4);
                                                //抽佣明细
                                                TbRaidDetails tbRaidDetails4 = new TbRaidDetails();
                                                tbRaidDetails4.setAmount(money4);
                                                tbRaidDetails4.setBenefitUserid(user4.getUserId());
                                                tbRaidDetails4.setBenefitUsername(user4.getUsername());
                                                tbRaidDetails4.setTriggerUserid(user0.getUserId());
                                                tbRaidDetails4.setTriggerUsername(user0.getUsername());
                                                tbRaidDetails4.setTriggerImg(user0.getProfileimageurl());
                                                tbRaidDetails4.setCreatetime(new Date());
                                                tbRaidDetails4.setMonth(calendar.get(Calendar.MONTH) + 1);
                                                tbRaidDetails4.setOrderid(order.getId());
                                                sysTbRaidDetailsService.save(tbRaidDetails4);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    }
                }
            } else if (user00.getType() == 1) {
                /*
                复购
                 */
                //订单金额
                Float money = order.getTotalfee() - order.getFreight();
                //发起者 E
                TbUser user0 = tbUserService.queryByUid(order.getUid());
                TbCommissionRatio tbCommissionRatio = sysCommissionRatioService.queryByUserOld();
                if (user0 != null) {
                    if(user0.getParentid() != null) {
                        //第一级受益者
                        TbUser user1 = tbUserService.queryById(user0.getParentid());
                        if (user1 != null) {
                            //计算第一级受益者应得金额
                            Integer money1 = (int) (money * tbCommissionRatio.getRatio());
                            user1.setMemberbalance(user1.getMemberbalance() + money1);
                            user1.setBalance(user1.getBalance() + money1);
                            tbUserService.update(user1);
                            //抽佣明细
                            TbRaidDetails tbRaidDetails1 = new TbRaidDetails();
                            tbRaidDetails1.setAmount(money1);
                            tbRaidDetails1.setBenefitUserid(user1.getUserId());
                            tbRaidDetails1.setBenefitUsername(user1.getUsername());
                            tbRaidDetails1.setTriggerUserid(user0.getUserId());
                            tbRaidDetails1.setTriggerUsername(user0.getUsername());
                            tbRaidDetails1.setTriggerImg(user0.getProfileimageurl());
                            tbRaidDetails1.setCreatetime(new Date());
                            tbRaidDetails1.setMonth(calendar.get(Calendar.MONTH) + 1);
                            tbRaidDetails1.setOrderid(order.getId());
                            sysTbRaidDetailsService.save(tbRaidDetails1);
                            if (user1.getParentid() != null) {
                                //第二级受益者
                                TbUser user2 = tbUserService.queryById(user1.getParentid());
                                if (user2 != null) {
                                    Integer money2 = (int) (money * tbCommissionRatio.getSecondratio());
                                    user2.setMemberbalance(user2.getMemberbalance() + money2);
                                    user2.setBalance(user2.getBalance() + money2);
                                    tbUserService.update(user2);
                                    //抽佣明细
                                    TbRaidDetails tbRaidDetails2 = new TbRaidDetails();
                                    tbRaidDetails2.setAmount(money2);
                                    tbRaidDetails2.setBenefitUserid(user2.getUserId());
                                    tbRaidDetails2.setBenefitUsername(user2.getUsername());
                                    tbRaidDetails2.setTriggerUserid(user0.getUserId());
                                    tbRaidDetails2.setTriggerUsername(user0.getUsername());
                                    tbRaidDetails2.setTriggerImg(user0.getProfileimageurl());
                                    tbRaidDetails2.setCreatetime(new Date());
                                    tbRaidDetails2.setMonth(calendar.get(Calendar.MONTH) + 1);
                                    tbRaidDetails2.setOrderid(order.getId());
                                    sysTbRaidDetailsService.save(tbRaidDetails2);
                /*-------------------------------------------------------------------------------*/
                                    if (user2.getParentid() != null) {
                                        //第三级受益者
                                        TbUser user3 = tbUserService.queryById(user2.getParentid());
                                        if (user3 != null) {
                                            Integer money3 = (int) ((money1 + money2) * tbCommissionRatio.getThirdratio());
                                            user3.setMemberbalance(user3.getMemberbalance() + money3);
                                            user3.setBalance(user3.getBalance() + money3);
                                            tbUserService.update(user3);
                                            //抽佣明细
                                            TbRaidDetails tbRaidDetails3 = new TbRaidDetails();
                                            tbRaidDetails3.setAmount(money3);
                                            tbRaidDetails3.setBenefitUserid(user3.getUserId());
                                            tbRaidDetails3.setBenefitUsername(user3.getUsername());
                                            tbRaidDetails3.setTriggerUserid(user0.getUserId());
                                            tbRaidDetails3.setTriggerUsername(user0.getUsername());
                                            tbRaidDetails3.setTriggerImg(user0.getProfileimageurl());
                                            tbRaidDetails3.setCreatetime(new Date());
                                            tbRaidDetails3.setMonth(calendar.get(Calendar.MONTH) + 1);
                                            tbRaidDetails3.setOrderid(order.getId());
                                            sysTbRaidDetailsService.save(tbRaidDetails3);

                                            if (user3.getParentid() != null) {
                                                //第四级受益者
                                                TbUser user4 = tbUserService.queryById(user3.getParentid());
                                                if (user4 != null) {
                                                    Integer money4 = (int) (money2 * tbCommissionRatio.getFourthratio());
                                                    user4.setMemberbalance(user4.getMemberbalance() + money4);
                                                    user4.setBalance(user4.getBalance() + money4);
                                                    tbUserService.update(user4);
                                                    //抽佣明细
                                                    TbRaidDetails tbRaidDetails4 = new TbRaidDetails();
                                                    tbRaidDetails4.setAmount(money4);
                                                    tbRaidDetails4.setBenefitUserid(user4.getUserId());
                                                    tbRaidDetails4.setBenefitUsername(user4.getUsername());
                                                    tbRaidDetails4.setTriggerUserid(user0.getUserId());
                                                    tbRaidDetails4.setTriggerUsername(user0.getUsername());
                                                    tbRaidDetails4.setTriggerImg(user0.getProfileimageurl());
                                                    tbRaidDetails4.setCreatetime(new Date());
                                                    tbRaidDetails4.setMonth(calendar.get(Calendar.MONTH) + 1);
                                                    tbRaidDetails4.setOrderid(order.getId());
                                                    sysTbRaidDetailsService.save(tbRaidDetails4);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            lock.unlock();
        }
    }


    /**
     * 代理抽佣二级复二级分销结算
     *
     * @param
     * @return
     * @// TODO: 2018/3/20 佣金明细保存
     */
    public boolean distribution2(TbReplenish tbReplenish) {
        ReentrantLock lock = new ReentrantLock();
        Calendar calendar = Calendar.getInstance();
        try {
            lock.lock();
            System.out.println("进入代理抽佣");
            System.out.println("进货单金额："+tbReplenish.getMoney());
            //获取订单金额
            Float money = Float.valueOf(tbReplenish.getMoney());
            System.out.println("进货单金额2："+money);
            //发起者 E
            TbUser tbUser0 = tbUserService.queryById(tbReplenish.getUserid());
            System.out.println("发起者："+tbUser0.getUsername());
            TbCommissionRatio tbCommissionRatio = sysCommissionRatioService.queryByProxy();
            System.out.println("代理抽佣比例："+tbCommissionRatio.getThirdratio());
            System.out.println("代理抽佣比例："+tbCommissionRatio.getSecondratio());
            System.out.println("代理抽佣比例："+tbCommissionRatio.getFourthratio());
            if (tbUser0 != null) {
                if(tbUser0.getParentid() != null) {
                    //第一级收益者
                    TbUser tbUser1 = tbUserService.queryById(tbUser0.getParentid());
                    if (tbUser1 != null) {
                        System.out.println("第一级受益者：" + tbUser1.getUsername());
                        //计算第一级受益者应得金额
                        Integer money1 = (int) (money * tbCommissionRatio.getRatio());
                        System.out.println("第一级受益者应得金额：" + money1);
                        tbUser1.setProxybalance(tbUser1.getProxybalance() + money1);
                        tbUser1.setBalance(tbUser1.getBalance() + money1);
                        System.out.println("第一级受益者余额：" + tbUser1.getBalance());
                        int result = tbUserService.update(tbUser1);
                        System.out.println("第一级受益者执行成功：" + result);
                        //抽佣明细
                        TbProxyDetails tbProxyDetails1 = new TbProxyDetails();
                        tbProxyDetails1.setOrderid(tbReplenish.getId());
                        tbProxyDetails1.setAmount(money1);
                        tbProxyDetails1.setCreatetime(new Date());
                        tbProxyDetails1.setMonth(calendar.get(Calendar.MONTH) + 1);
                        tbProxyDetails1.setBenefitUserid(tbUser1.getUserId());
                        tbProxyDetails1.setBenefitUsername(tbUser1.getUsername());
                        tbProxyDetails1.setTriggerUserid(tbUser0.getUserId());
                        tbProxyDetails1.setTriggerUsername(tbUser0.getUsername());
                        tbProxyDetails1.setTriggerImg(tbUser0.getProfileimageurl());
                        tbProxyDetails1.setOrderid(tbReplenish.getId());
                        tbProxyDetailsService.insert(tbProxyDetails1);
                        if(tbUser1.getParentid() != null) {
                        //第二级受益者
                        TbUser tbUser2 = tbUserService.queryById(tbUser1.getParentid());
                        if (tbUser2 != null) {
                            System.out.println("第二级受益者：" + tbUser2.getUsername());
                            //计算第二级受益者应得金额
                            Integer money2 = (int) (money * tbCommissionRatio.getSecondratio());
                            System.out.println("第二级受益者应得金额：" + money2);
                            tbUser2.setProxybalance(tbUser2.getProxybalance() + money2);
                            tbUser2.setBalance(tbUser2.getBalance() + money2);
                            System.out.println("第二级受益者余额：" + tbUser2.getBalance());
                            int result2 = tbUserService.update(tbUser2);
                            System.out.println("第二级受益者执行成功：" + result2);
                            //抽佣明细
                            TbProxyDetails tbProxyDetails2 = new TbProxyDetails();
                            tbProxyDetails2.setOrderid(tbReplenish.getId());
                            tbProxyDetails2.setAmount(money2);
                            tbProxyDetails2.setCreatetime(new Date());
                            tbProxyDetails2.setMonth(calendar.get(Calendar.MONTH) + 1);
                            tbProxyDetails2.setBenefitUserid(tbUser2.getUserId());
                            tbProxyDetails2.setBenefitUsername(tbUser2.getUsername());
                            tbProxyDetails2.setTriggerUserid(tbUser0.getUserId());
                            tbProxyDetails2.setTriggerUsername(tbUser0.getUsername());
                            tbProxyDetails2.setTriggerImg(tbUser0.getProfileimageurl());
                            tbProxyDetails2.setOrderid(tbReplenish.getId());
                            tbProxyDetailsService.insert(tbProxyDetails2);
                            if (tbUser2.getParentid() != null) {
                                //第三级受益者
                                TbUser tbUser3 = tbUserService.queryById(tbUser2.getParentid());
                                if (tbUser3 != null) {
                                    System.out.println("第三级受益者：" + tbUser3.getUsername());
                                    //计算第三级受益者应得金额(第一级和第二级收益和的1%)
                                    Integer money3 = (int) ((money1 + money2) * tbCommissionRatio.getThirdratio());
                                    System.out.println("应得金额：" + money3);
                                    tbUser3.setProxybalance(tbUser3.getProxybalance() + money3);
                                    tbUser3.setBalance(tbUser3.getBalance() + money3);
                                    System.out.println("受益者应得金额：" + tbUser3.getBalance());
                                    int result3 = tbUserService.update(tbUser3);
                                    System.out.println("执行结果：" + result3);
                                    //抽佣明细
                                    TbProxyDetails tbProxyDetails3 = new TbProxyDetails();
                                    tbProxyDetails3.setOrderid(tbReplenish.getId());
                                    tbProxyDetails3.setAmount(money3);
                                    tbProxyDetails3.setCreatetime(new Date());
                                    tbProxyDetails3.setMonth(calendar.get(Calendar.MONTH) + 1);
                                    tbProxyDetails3.setBenefitUserid(tbUser3.getUserId());
                                    tbProxyDetails3.setBenefitUsername(tbUser3.getUsername());
                                    tbProxyDetails3.setTriggerUserid(tbUser0.getUserId());
                                    tbProxyDetails3.setTriggerUsername(tbUser0.getUsername());
                                    tbProxyDetails3.setTriggerImg(tbUser0.getProfileimageurl());
                                    tbProxyDetails3.setOrderid(tbReplenish.getId());
                                    tbProxyDetailsService.insert(tbProxyDetails3);
                                    if (tbUser3.getParentid() != null) {
                                        //第四级受益者
                                        TbUser tbUser4 = tbUserService.queryById(tbUser3.getParentid());
                                        if (tbUser4 != null) {
                                            System.out.println("第四级受益者：" + tbUser4.getUsername());
                                            //计算第四级受益者应得金额(第二级收益和的1%)
                                            Integer money4 = (int) (money2 * tbCommissionRatio.getFourthratio());
                                            System.out.println("第四级受益者应得金额：" + money4);
                                            tbUser4.setProxybalance(tbUser4.getProxybalance() + money4);
                                            tbUser4.setBalance(tbUser4.getBalance() + money4);
                                            System.out.println("第四级受益者应得金额：" + tbUser4.getBalance());
                                            int result4 = tbUserService.update(tbUser4);
                                            System.out.println("执行结果：" + result4);
                                            //抽佣明细
                                            TbProxyDetails tbProxyDetails4 = new TbProxyDetails();
                                            tbProxyDetails4.setOrderid(tbReplenish.getId());
                                            tbProxyDetails4.setAmount(money4);
                                            tbProxyDetails4.setCreatetime(new Date());
                                            tbProxyDetails4.setMonth(calendar.get(Calendar.MONTH) + 1);
                                            tbProxyDetails4.setBenefitUserid(tbUser4.getUserId());
                                            tbProxyDetails4.setBenefitUsername(tbUser4.getUsername());
                                            tbProxyDetails4.setTriggerUserid(tbUser0.getUserId());
                                            tbProxyDetails4.setTriggerUsername(tbUser0.getUsername());
                                            tbProxyDetails4.setTriggerImg(tbUser0.getProfileimageurl());
                                            tbProxyDetails4.setOrderid(tbReplenish.getId());
                                            tbProxyDetailsService.insert(tbProxyDetails4);
                                        }
                                    }
                                }
                            }
                        }
                        }
                    }
                }
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            lock.unlock();
        }
    }


    /**
     * 团购分销抽佣结算
     *
     * @param
     * @return
     */
    public boolean distribution3(TbOrder tbOrder) {
        ReentrantLock lock = new ReentrantLock();
        Calendar calendar = Calendar.getInstance();
        try {
            lock.lock();
            //订单金额
            Float money = tbOrder.getTotalfee() - tbOrder.getFreight();
            //发起人
            TbUser tbUser0 = tbUserService.queryByUid(tbOrder.getUid());
            //第一级受益人
            TbUser tbUser1 = tbUserService.queryById(tbUser0.getParentid());
            //计算第一级受益人享受金额
            Integer money1 = (int) (money * 0.02f);
            tbUser1.setMemberbalance(tbUser1.getMemberbalance() + money1);
            tbUser1.setBalance(tbUser1.getBalance() + money1);
            tbUserService.update(tbUser1);
            //抽佣明细
            TbRaidDetails tbRaidDetails = new TbRaidDetails();
            tbRaidDetails.setAmount(money1);
            tbRaidDetails.setBenefitUserid(tbUser1.getUserId());
            tbRaidDetails.setBenefitUsername(tbUser1.getUsername());
            tbRaidDetails.setTriggerUserid(tbUser0.getUserId());
            tbRaidDetails.setTriggerUsername(tbUser0.getUsername());
            tbRaidDetails.setTriggerImg(tbUser0.getProfileimageurl());
            tbRaidDetails.setCreatetime(new Date());
            tbRaidDetails.setMonth(calendar.get(Calendar.MONTH) + 1);
            tbRaidDetails.setOrderid(tbOrder.getId());
            sysTbRaidDetailsService.save(tbRaidDetails);
            //第二级受益人
            TbUser tbUser2 = tbUserService.queryById(tbUser1.getParentid());
            if (tbUser2 != null) {
                //计算第二级受益人享受金额
                Integer money2 = (int) (money1 * 0.01f);
                tbUser2.setMemberbalance(tbUser2.getMemberbalance() + money2);
                tbUser2.setBalance(tbUser2.getBalance() + money2);
                tbUserService.update(tbUser2);
                //抽佣明细
                TbRaidDetails tbRaidDetails1 = new TbRaidDetails();
                tbRaidDetails1.setAmount(money1);
                tbRaidDetails1.setBenefitUserid(tbUser2.getUserId());
                tbRaidDetails1.setBenefitUsername(tbUser2.getUsername());
                tbRaidDetails1.setTriggerUserid(tbUser0.getUserId());
                tbRaidDetails1.setTriggerUsername(tbUser0.getUsername());
                tbRaidDetails1.setTriggerImg(tbUser0.getProfileimageurl());
                tbRaidDetails1.setCreatetime(new Date());
                tbRaidDetails1.setMonth(calendar.get(Calendar.MONTH) + 1);
                tbRaidDetails1.setOrderid(tbOrder.getId());
                sysTbRaidDetailsService.save(tbRaidDetails1);
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            lock.unlock();
        }
        return true;
    }


    /**
     * 魅力值/等两只积分二级分销制度
     *
     * @return
     */
    public boolean distribution4(TbOrder tbOrder) {
        TbUser tbUser = tbUserService.queryByUid(tbOrder.getUid());
//        if(!tbOrder.getChannel().equals("6")) {
        ReentrantLock lock = new ReentrantLock();
        Calendar calendar = Calendar.getInstance();
        try {
            lock.lock();
            //订单金额
            Float money = (tbOrder.getTotalfee() - tbOrder.getFreight()) / 100;
            //对应积分
            Integer jifen = (int) (money + 0);
            //发起人
            TbUser tbUser0 = tbUserService.queryByUid(tbOrder.getUid());
            if (tbUser0 != null) {
                tbUser0.setCharmvalue(tbUser0.getCharmvalue() + jifen);
                tbUserService.update(tbUser0);
                //积分等级换算
                integralClearingUtil.distribution5(tbUser0);
                //积分收益明细
                TbCharmvalueNotes tbCharmvalueNotes0 = new TbCharmvalueNotes();
                tbCharmvalueNotes0.setCreattime(new Date());
                tbCharmvalueNotes0.setValue(jifen);
                tbCharmvalueNotes0.setType(0);
                tbCharmvalueNotes0.setUid(tbUser0.getUuid());
                tbCharmvalueNotes0.setOrderid(tbOrder.getId());
                tbCharmvalueNotesService.save(tbCharmvalueNotes0);


                //第一级受益人
                TbUser tbUser1 = tbUserService.queryById(tbUser0.getParentid());
                if (tbUser1 != null) {
                    //计算第一级受益人享受金额
                    tbUser1.setCharmvalue(tbUser1.getCharmvalue() + jifen);
                    tbUserService.update(tbUser1);
                    //积分等级换算
                    integralClearingUtil.distribution5(tbUser1);
                    //积分收益明细
                    TbCharmvalueNotes tbCharmvalueNotes1 = new TbCharmvalueNotes();
                    tbCharmvalueNotes1.setCreattime(new Date());
                    tbCharmvalueNotes1.setValue(jifen);
                    tbCharmvalueNotes1.setType(0);
                    tbCharmvalueNotes1.setUid(tbUser1.getUuid());
                    tbCharmvalueNotes1.setOrderid(tbOrder.getId());
                    tbCharmvalueNotesService.save(tbCharmvalueNotes1);

                    //第二级受益人
                    TbUser tbUser2 = tbUserService.queryById(tbUser1.getParentid());
                    if (tbUser2 != null) {
                        tbUser2.setCharmvalue(tbUser2.getCharmvalue() + jifen);
                        tbUserService.update(tbUser2);
                        //积分等级换算
                        integralClearingUtil.distribution5(tbUser2);
                        //积分收益明细
                        TbCharmvalueNotes tbCharmvalueNotes2 = new TbCharmvalueNotes();
                        tbCharmvalueNotes2.setCreattime(new Date());
                        tbCharmvalueNotes2.setValue(jifen);
                        tbCharmvalueNotes2.setType(0);
                        tbCharmvalueNotes2.setUid(tbUser2.getUuid());
                        tbCharmvalueNotes2.setOrderid(tbOrder.getId());
                        tbCharmvalueNotesService.save(tbCharmvalueNotes2);
                        return true;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            lock.unlock();
        }
//        }
//        else{
//            ReentrantLock lock = new ReentrantLock();
//            Calendar calendar = Calendar.getInstance();
//            try {
//                lock.lock();
//                //订单金额
//                Float money = tbOrder.getTotalfee()/100;
//                //对应积分
//                Integer jifen = (int) (money + 0);
//                //发起人
//                TbUser tbUser0 = tbUserService.queryByUid(tbOrder.getUid());
//                if(tbUser0 != null) {
//                    tbUser0.setEnergyvalue(tbUser0.getEnergyvalue() + jifen);
//                    tbUserService.update(tbUser0);
//                    //积分收益明细
//                    TbEnergyvalueNotes tbEnergyvalueNotes0 = new TbEnergyvalueNotes();
//                    tbEnergyvalueNotes0.setCreattime(new Date());
//                    tbEnergyvalueNotes0.setValue(jifen);
//                    tbEnergyvalueNotes0.setType(0);
//                    tbEnergyvalueNotes0.setOrderid(0);
//                    tbEnergyvalueNotes0.setUid(tbUser0.getUuid());
//                    tbEnergyvalueNotes0.setOrderid(tbOrder.getId());
//                    tbEnergyvalueNotesService.save(tbEnergyvalueNotes0);
//
//
//                    //第一级受益人
//                    TbUser tbUser1 = tbUserService.queryById(tbUser0.getParentid());
//                    if(tbUser1 != null) {
//                        //计算第一级受益人享受金额
//                        tbUser1.setEnergyvalue(tbUser1.getEnergyvalue() + jifen);
//                        tbUserService.update(tbUser1);
//                        //积分收益明细
//                        TbEnergyvalueNotes tbEnergyvalueNotes1 = new TbEnergyvalueNotes();
//                        tbEnergyvalueNotes1.setCreattime(new Date());
//                        tbEnergyvalueNotes1.setValue(jifen);
//                        tbEnergyvalueNotes1.setType(0);
//                        tbEnergyvalueNotes1.setUid(tbUser1.getUuid());
//                        tbEnergyvalueNotes1.setOrderid(tbOrder.getId());
//                        tbEnergyvalueNotesService.save(tbEnergyvalueNotes1);
//                        //第二级受益人
//                        TbUser tbUser2 = tbUserService.queryById(tbUser1.getParentid());
//                        if(tbUser2 != null) {
//                            tbUser2.setEnergyvalue(tbUser2.getEnergyvalue() + jifen);
//                            tbUserService.update(tbUser2);
//                            //积分收益明细
//                            TbEnergyvalueNotes tbEnergyvalueNotes2 = new TbEnergyvalueNotes();
//                            tbEnergyvalueNotes2.setCreattime(new Date());
//                            tbEnergyvalueNotes2.setValue(jifen);
//                            tbEnergyvalueNotes2.setType(0);
//                            tbEnergyvalueNotes2.setOrderid(0);
//                            tbEnergyvalueNotes2.setUid(tbUser2.getUuid());
//                            tbEnergyvalueNotes2.setOrderid(tbOrder.getId());
//                            tbEnergyvalueNotesService.save(tbEnergyvalueNotes2);
//                            return true;
//                        }
//                    }
//                }
//            } catch (Exception e) {
//                e.printStackTrace();
//                return false;
//            } finally {
//                lock.unlock();
//            }
//        }
        return true;
    }

}
